define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'pollers'], function ($, undefined, Backend, Table, Form, pollers) {

    var Controller = {
        index: function () {
            // 点击开始生成
            $(document).on("click", "#generateStart", function () {
                // 初始化提示框
                $("#generate-process").html("开始初始化...");
                $("#generate-state-panel .progress-bar").css("width", "0");
                $("#generate-describe").html("开始初始化...");
                $("#generate-next").html("如果网页卡顿请点击<a id=\"generateNext\" href=\"javascript:\">继续操作</a>");

                // 获取表单参数开始轮询任务
                var formElement = $("#generate-form");
                pollers.start(formElement.attr("action"), formElement.serializeArray(), Controller.api.pollersCallback);

                // 点击继续监听事件
                $(document).on("click", "#generateNext", function () {
                    pollers.proceed();
                    return false;
                });

                // 显示提示面板
                $("#generate-state-panel").show();
                return false;
            });

            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            // 轮询回调
            pollersCallback: function (res) {
                $("#generate-process").html(res.msg);
                $("#generate-describe").html(res.desc);
                pollers.progressbar_value = Math.floor(res.step / res.total * 100);
                if(res.step >= res.total){
                    pollers.progressbar_value = 100;
                    $("#generate-next").html("恭喜，一键操作已完成！");
                }

                $(".generate-process .progress-bar").css("width", pollers.progressbar_value + "%");
            }
        }
    };

    return Controller;
});